﻿//***********************************************************************
// Assembly         : phpmk
// Author           : sy
// Created          : 06-14-2010
//
// Last Modified By : sy
// Last Modified On : 06-14-2010
// Description      : 
//
// Copyright        : (c) Another-D-Mention Software. All rights reserved.
//***********************************************************************
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using phpmk.ADM.Out;
using phpmk.ADM.Core.Configuration;
using phpmk.ADM.Core.Model;
using System.Threading;

namespace phpmk.ADM.Commands.Deploy
{
    class DeployCommand : ICommand
    {
        private StringDictionary arguments;
        private Output output = Output.GetInstance();

        public void Execute(StringDictionary args)
        {
            this.arguments = args;
            bool succeded = false;
            ConfigFile config = ConfigFile.GetInstance();

            output.Write("");
            output.Info("Staring Deploy Task");
            ProgressBar pb;

            foreach (Env env in config.Model.Deploy.Env) {
                output.Write("");
                output.Write("Deploying to [" + env.Name + "] environment");
                output.Separator();

                output.Write("Connecting to FTP...", true);
                output.SetStatus(ActionStatus.OK);
                pb = new ProgressBar(100, "Uploading files");
                for (int i = 0; i < 100; i++) {
                    pb.Increment();
                    Thread.Sleep(10);
                }
                pb.Complete();

                output.Write("Executing pre-deploy actions...", true);
                output.Info("No actions defined");
                output.SetStatus(ActionStatus.SKIPP);

                output.Write("Executing installer..", true);
                output.SetStatus(ActionStatus.OK);

                output.Write("Testing Database Connection..", true);
                output.SetStatus(ActionStatus.OK);

                output.Write("Installing DataBase..", true);
                output.SetStatus(ActionStatus.OK);

                output.Write("Executing post-deploy actions...", true);
                output.Info("No actions defined");
                output.SetStatus(ActionStatus.SKIPP);

                output.Separator();
                output.Info("DEPLOY SUCCESSFUL !");
                output.Write("2 Messages, 0 Warnings, 0 Errors");
            }


            succeded = true;

            if (!succeded) {
                throw new DeployException("DEPLOY FAILED !");
            }
        }

        public void Help()
        {
            
        }

        public string Info()
        {
            return "Overrides deploy directives from configuration file";
        }
    }

    public class DeployException : Exception
    {
        
        public DeployException()
        {
        }

        public DeployException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        public DeployException(string message, params string[] arguments)
            : base(String.Format(message, arguments))
        {
        }
    }
}
